package com.jilian.mydemo.modul.exam.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.WrongAnwserDto;
import com.jilian.mydemo.listener.CustomItemClickListener;

import java.util.List;


public class CheckWrongTopicAdapter extends RecyclerView.Adapter<CheckWrongTopicAdapter.ViewHolder> {
    private Activity mContext;
    private List<WrongAnwserDto> datas;
    private CustomItemClickListener listener;

    public CheckWrongTopicAdapter(Activity context, List<WrongAnwserDto> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_checkwrongtopic, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvContent.setText(datas.get(position).getSortNum()+"."+datas.get(position).getContent());
        holder.tvA.setText("A."+datas.get(position).getAnswerA());
        holder.tvB.setText("B."+datas.get(position).getAnswerB());
        holder.tvC.setText("C."+datas.get(position).getAnswerC());
        holder.tvD.setText("D."+datas.get(position).getAnswerD());
        //塞选正确答案
        if ("A".equals(datas.get(position).getCorrectAnswer())) {
            holder.tvA.setTextColor(mContext.getResources().getColor(R.color.color_main_title));
            holder.rlOne.setBackgroundResource(R.drawable.shape_main_dark_100);
            holder.ivWrongOne.setVisibility(View.GONE);
        }
        //塞选正确答案
        else if ("A".equals(datas.get(position).getWrongAnswer())) {
            holder.tvA.setTextColor(mContext.getResources().getColor(R.color.color_red));
            holder.rlOne.setBackgroundResource(R.drawable.shape_red_dark_100);
            holder.ivWrongOne.setVisibility(View.VISIBLE);
        } else {
            holder.tvA.setTextColor(mContext.getResources().getColor(R.color.color_text));
            holder.rlOne.setBackgroundResource(R.drawable.shape_dark_100);
            holder.ivWrongOne.setVisibility(View.GONE);
        }


        if ("B".equals(datas.get(position).getCorrectAnswer())) {
            holder.tvB.setTextColor(mContext.getResources().getColor(R.color.color_main_title));
            holder.rlTwo.setBackgroundResource(R.drawable.shape_main_dark_100);
            holder.ivWrongTwo.setVisibility(View.GONE);
        } else if ("B".equals(datas.get(position).getWrongAnswer())) {
            holder.tvB.setTextColor(mContext.getResources().getColor(R.color.color_red));
            holder.rlTwo.setBackgroundResource(R.drawable.shape_red_dark_100);
            holder.ivWrongTwo.setVisibility(View.VISIBLE);
        } else {
            holder.tvB.setTextColor(mContext.getResources().getColor(R.color.color_text));
            holder.rlTwo.setBackgroundResource(R.drawable.shape_dark_100);
            holder.ivWrongTwo.setVisibility(View.GONE);
        }

        if ("C".equals(datas.get(position).getCorrectAnswer())) {
            holder.tvC.setTextColor(mContext.getResources().getColor(R.color.color_main_title));
            holder.rlThree.setBackgroundResource(R.drawable.shape_main_dark_100);
            holder.ivWrongThree.setVisibility(View.GONE);
        } else if ("C".equals(datas.get(position).getWrongAnswer())) {
            holder.tvC.setTextColor(mContext.getResources().getColor(R.color.color_red));
            holder.rlThree.setBackgroundResource(R.drawable.shape_red_dark_100);
            holder.ivWrongThree.setVisibility(View.VISIBLE);
        } else {
            holder.tvC.setTextColor(mContext.getResources().getColor(R.color.color_text));
            holder.rlThree.setBackgroundResource(R.drawable.shape_dark_100);
            holder.ivWrongThree.setVisibility(View.GONE);
        }

        if ("D".equals(datas.get(position).getCorrectAnswer())) {
            holder.tvD.setTextColor(mContext.getResources().getColor(R.color.color_main_title));
            holder.rlFour.setBackgroundResource(R.drawable.shape_main_dark_100);
            holder.ivWrongThree.setVisibility(View.GONE);
        } else if ("D".equals(datas.get(position).getWrongAnswer())) {
            holder.tvD.setTextColor(mContext.getResources().getColor(R.color.color_red));
            holder.rlFour.setBackgroundResource(R.drawable.shape_red_dark_100);
            holder.ivWrongThree.setVisibility(View.VISIBLE);
        } else {
            holder.tvD.setTextColor(mContext.getResources().getColor(R.color.color_text));
            holder.rlFour.setBackgroundResource(R.drawable.shape_dark_100);
            holder.ivWrongFour.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvContent;
        private ImageView ivWrongOne;
        private ImageView ivWrongTwo;
        private ImageView ivWrongThree;
        private ImageView ivWrongFour;

        private TextView tvA;
        private TextView tvB;
        private TextView tvC;
        private TextView tvD;

        private RelativeLayout rlOne;
        private RelativeLayout rlTwo;
        private RelativeLayout rlThree;
        private RelativeLayout rlFour;



        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
            ivWrongOne = (ImageView) itemView.findViewById(R.id.iv_wrong_one);
            ivWrongTwo = (ImageView) itemView.findViewById(R.id.iv_wrong_two);
            ivWrongThree = (ImageView) itemView.findViewById(R.id.iv_wrong_three);
            ivWrongFour = (ImageView) itemView.findViewById(R.id.iv_wrong_four);

            tvA = (TextView) itemView.findViewById(R.id.tv_A);
            tvB = (TextView) itemView.findViewById(R.id.tv_B);
            tvC = (TextView) itemView.findViewById(R.id.tv_C);
            tvD = (TextView) itemView.findViewById(R.id.tv_D);

            rlOne = (RelativeLayout) itemView.findViewById(R.id.rl_one);
            rlTwo = (RelativeLayout) itemView.findViewById(R.id.rl_two);
            rlThree = (RelativeLayout) itemView.findViewById(R.id.rl_three);
            rlFour = (RelativeLayout) itemView.findViewById(R.id.rl_four);

        }

    }
}
